package com.example.app.pages.main.home

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBarItemColors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.example.app.barStateViewModel
import com.example.app.components.MyDivider.MyDivider
import com.example.app.components.MyDivider.MyDividerDirection
import com.example.app.components.MyIcon.MyIcon
import com.example.app.components.MyNavigationBar.MyNavigationBar
import com.example.app.components.MyNavigationBar.MyNavigationBarItem
import com.example.app.components.MyScaffold.MyScaffoldHasTipAndLoading
import com.example.app.components.MySurface.MySurface
import com.example.app.components.MyText.MyText
import com.example.app.mainViewModel
import com.example.app.pages.main.home.accountList.AccountList
import com.example.app.pages.main.home.accountList.viewModel.AccountListViewModel
import com.example.app.pages.main.home.addNew.AddNew
import com.example.app.pages.main.home.addNew.addRecord.viewModel.AddRecordViewModel
import com.example.app.pages.main.home.justTestPage.JustTestPage
import com.example.app.pages.main.home.me.Me
import com.example.app.pages.main.home.me.viewModel.AddNewViewModel
import com.example.app.pages.main.home.viewModel.HomeViewModel
import com.example.app.routeViewModel
import com.example.app.viewmodel.BarStateViewModel
import com.example.app.viewmodel.HomeChild
import com.example.app.viewmodel.MainViewModel
import com.example.app.viewmodel.RouteViewModel
import kotlinx.coroutines.CoroutineScope

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun Home(

) {
    var scope = rememberCoroutineScope()
    var curHomeChild = routeViewModel.curHomeChild.collectAsState()
    var showBottomBar = barStateViewModel.showBottomBar.collectAsState()
    MyScaffoldHasTipAndLoading(

        bottomBar = {
            if (showBottomBar.value) {
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                ) {
                    Column(
                    ) {
                        MyDivider(
                            myDividerDirection = MyDividerDirection.HOR,
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(1.dp)
                        )
                        MyNavigationBar(content = {
                            HomeChild.values().forEachIndexed { index, homeChild ->
                                if (homeChild.title == HomeChild.ADD_NEW.title) {
                                    MyNavigationBarItem(
                                        modifier = Modifier,
                                        colors = NavigationBarItemColors(
                                            selectedIconColor = Color.Transparent,
                                            selectedTextColor = Color.Transparent,
                                            selectedIndicatorColor = Color.Transparent,
                                            unselectedIconColor = Color.Transparent,
                                            unselectedTextColor = Color.Transparent,
                                            disabledIconColor = Color.Transparent,
                                            disabledTextColor = Color.Transparent,
                                        ),
                                        selected = homeChild.title === curHomeChild.value.title,
                                        onClick = {
                                            routeViewModel.flowUpdate_curHomeChild(
                                                homeChild
                                            )
                                        },
                                        icon = {
                                            MyIcon(
                                                modifier = Modifier
                                                    .clip(CircleShape)
                                                    .size(50.dp)
                                                    .background(MaterialTheme.colorScheme.primary),
                                                imageVector = homeChild.icon,
                                                tint = MaterialTheme.colorScheme.surface
                                            )
                                        })
                                } else {
                                    MyNavigationBarItem(colors = NavigationBarItemColors(
                                        selectedIconColor = Color.Transparent,
                                        selectedTextColor = Color.Transparent,
                                        selectedIndicatorColor = Color.Transparent,
                                        unselectedIconColor = Color.Transparent,
                                        unselectedTextColor = Color.Transparent,
                                        disabledIconColor = Color.Transparent,
                                        disabledTextColor = Color.Transparent,
                                    ),
                                        selected = homeChild.title === curHomeChild.value.title,
                                        onClick = {
                                            routeViewModel.flowUpdate_curHomeChild(
                                                homeChild
                                            )
                                        },
                                        icon = {
                                            MyIcon(
                                                imageVector = homeChild.icon,
                                                tint = if (homeChild.title === curHomeChild.value.title) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.inversePrimary
                                            )
                                        },
                                        label = {
                                            MyText(
                                                text = homeChild.title,
                                                color = if (homeChild.title === curHomeChild.value.title) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.inversePrimary,
                                                style = MaterialTheme.typography.bodyMedium.copy(
                                                    fontWeight = if (homeChild.title === curHomeChild.value.title) FontWeight.Light else FontWeight.Medium
                                                )
                                            )
                                        })
                                }
                            }
                        })
                    }
                }
            }
        },
        content = {
            MySurface(
                modifier = Modifier.fillMaxSize(),
                content = {
                    Box {
                        when (curHomeChild.value) {
                            HomeChild.ME -> {
                                Me()
                            }

                            HomeChild.ADD_NEW -> {
                                AddNew()
                            }

                            HomeChild.ACCOUNT_LIST -> {
                                AccountList()
                            }

                            HomeChild.JUST_TEST_PAGE -> {
                                JustTestPage()
                            }

                            else -> {
                            }
                        }
                    }
                })
        })
}